﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using TouchRC.Classes;
using TouchRC.BarManagers;
using TouchRC.MetadataCaching;
using TouchRC.ListBoxes;
using Fluid.Controls;
using System.Drawing;
using TouchRC.Logic;
using TouchRC.Custom_Controls;
using TouchRC.Custom_Controls.Events;

namespace TouchRC
{
    public class MediaBrowserPanel : FluidPanelBase
    {
        #region Private Members

        private MetadataCollection m_Collection;
        private MetadataListBox m_Listbox;
        private AlphabetPanel m_LeftAlphabet;
        private AlphabetPanel m_RightAlphabet;
        private bool m_ShowAlphabet = false;
        private List<int> m_Memory = new List<int>();
        private TouchButton m_BackButton;

        #endregion

        public MediaBrowserPanel(MetadataCollection collection)
        {
            m_Collection = collection;
            UpdateView(collection, collection.Name, 0);
        }

        #region FluidPanelBase abstract methods

        public override void SetControlPositions(ScreenOrientation screenOrientation)
        {
            int listboxWidth = ScreenWidth - (int)(ScreenWidth * 0.2);
            int margin = (ScreenWidth - listboxWidth) / 2;

            m_Listbox.Bounds = new Rectangle(margin, BaseControlHeight, listboxWidth, ScreenHeight - (2 * BaseControlHeight));

            int alphaWidth = margin;
            int leftHeight = AppSettings.ScreenResolution.Height - AppSettings.BaseControlHeight;
            int rightX = m_Listbox.Left + m_Listbox.Width;
            m_RightAlphabet.Bounds = new Rectangle(rightX, 0, alphaWidth,
                m_RightAlphabet.ScaleY(AppSettings.ScreenResolution.Height));

            m_RightAlphabet.SetButtonPositions();

            m_LeftAlphabet.Bounds = new Rectangle(0, 0, alphaWidth,
                m_LeftAlphabet.ScaleY(leftHeight));

            m_LeftAlphabet.SetButtonPositions();
        }

        public override void RefreshValues()
        {

        }

        public override void AddControls()
        {
            Controls.Add(m_Listbox);
            Controls.Add(m_RightAlphabet);
            Controls.Add(m_LeftAlphabet);
            Controls.Add(m_BackButton);
        }

        public override void InitPanelControls()
        {
            m_Listbox = new MetadataListBox();
            m_BackButton = new TouchButton();

            #region Left Letters
            List<string> left = new List<string>();
            left.Add("#");
            left.Add("A");
            left.Add("B");
            left.Add("C");
            left.Add("D");
            left.Add("E");
            left.Add("F");
            left.Add("G");
            left.Add("H");
            left.Add("I");
            left.Add("J");
            left.Add("K");

            #endregion
            #region Right letters
            List<string> right = new List<string>();
            right.Add("L");
            right.Add("M");
            right.Add("N");
            right.Add("O");
            right.Add("P");
            right.Add("Q");
            right.Add("R");
            right.Add("S");
            right.Add("T");
            right.Add("U");
            right.Add("V");
            right.Add("W");
            right.Add("X");
            right.Add("Y");
            right.Add("Z");

            #endregion

            m_LeftAlphabet = new AlphabetPanel(left.ToArray());
            m_RightAlphabet = new AlphabetPanel(right.ToArray());
        }

        public override List<BarOption> GetBarOptions()
        {
            BarOption alphabet = new BarOption();
            alphabet.Image = ImageFactory.GetBitmap(ImageTypes.Plus);
            alphabet.Click += new EventHandler<EventArgs>(alphabet_Click);
            return new List<BarOption>() { alphabet };
        }

        void alphabet_Click(object sender, EventArgs e)
        {
            HideOptionsBar();
            m_ShowAlphabet = !m_ShowAlphabet;

            if (m_ShowAlphabet)
                ShowAlphabet();
            else
                HideAlphabet();

            m_LeftAlphabet.Visible = m_ShowAlphabet;
            m_RightAlphabet.Visible = m_ShowAlphabet;
            base.Invalidate();
        }

        #endregion

        protected override void InitControl()
        {
            base.InitControl();

            m_Listbox.ItemClick += new EventHandler<ListBoxItemEventArgs>(m_Listbox_ItemClick);
            m_Listbox.Anchor = AnchorAll;
            base.ShowMenuButton = true;

            m_RightAlphabet.OriginalTop = 0;
            m_LeftAlphabet.OriginalTop = 0;
            m_LeftAlphabet.Visible = m_ShowAlphabet;
            m_RightAlphabet.Visible = m_ShowAlphabet;

            m_LeftAlphabet.AlphaSelected += new EventHandler<AlphaSelectedEventArgs>(alphaSelected);
            m_RightAlphabet.AlphaSelected += new EventHandler<AlphaSelectedEventArgs>(alphaSelected);
        }

        void alphaSelected(object sender, AlphaSelectedEventArgs e)
        {
            AlphabetSplashPanel alphaSplash = new AlphabetSplashPanel();
            alphaSplash.ShowLabelWithLetter(e.Value);
            m_Listbox.ScrollToLetter(e.Value);
        }

        void m_Listbox_ItemClick(object sender, ListBoxItemEventArgs e)
        {
            IMediaName media = e.Item as IMediaName;
            if (media != null)
            {
                LoadMediaName(media, e.ItemIndex);
                // do something clever...
            }
        }

        private void LoadMediaName(IMediaName media, int index)
        {
            if (media is VirtualCache)
            {
                VirtualCache cache = media as VirtualCache;
                if (cache != null)
                {
                    Forward(index);
                    UpdateView((List<IMediaName>)cache, cache.Name, 0);
                }
            }
            else if (media is MovieSummary)
            {
                MovieSummary movie = media as MovieSummary;
                if (movie != null)
                {
                    MetadataPanel.ShowMetadataPanel(movie, true);
                }
            }
            else if (media is TvShowSummary)
            {
                TvShowSummary tvShow = media as TvShowSummary;
                if (tvShow != null)
                {
                    Forward(index);
                    UpdateView(tvShow.Seasons, tvShow.Name, 0);
                }
            }
            else if (media is SeasonSummary)
            {
                SeasonSummary season = media as SeasonSummary;
                if (season != null)
                {
                    Forward(index);
                    UpdateView(season.Episodes, season.Name, 0);
                }
            }
            else if (media is EpisodeSummary)
            {
                EpisodeSummary episode = media as EpisodeSummary;
                if (episode != null)
                {
                    MetadataPanel.ShowMetadataPanel(episode, true);
                }
            }
        }

        private void ShowAlphabet()
        {
            m_LeftAlphabet.Show(ShowTransition.FromTop);
            m_RightAlphabet.Show(ShowTransition.FromTop);
        }

        private void HideAlphabet()
        {
            m_LeftAlphabet.Close(ShowTransition.FromTop);
            m_RightAlphabet.Close(ShowTransition.FromTop);
        }

        private void Forward(int selectedRank)
        {
            m_Memory.Add(selectedRank);
        }

        public override void Back()
        {
            int count = m_Memory.Count;
            if (count > 0)
            {
                List<IMediaName> lastMediaName = m_Collection;
                string currentName = m_Collection.Name;
                for (int i = 0; i < count - 1; i++)
                {
                    int mem = m_Memory[i];
                    List<IMediaName> tempList = null;
                    IMediaName name = null;
                    var bob = lastMediaName[mem];
                    if (bob is VirtualCache)
                    {
                        tempList = ((List<IMediaName>)(VirtualCache)bob);
                        name = ((IMediaName)(VirtualCache)bob);
                    }
                    else if (bob is TvShowSummary)
                    {
                        TvShowSummary tvShow = bob as TvShowSummary;
                        tempList = (List<IMediaName>)(tvShow.Seasons);
                        name = ((IMediaName)(TvShowSummary)bob);
                    }
                    else if (bob is SeasonSummary)
                    {
                        SeasonSummary season = bob as SeasonSummary;
                        tempList = (List<IMediaName>)(season.Episodes);
                        name = ((IMediaName)(SeasonSummary)bob);
                    }

                    if (tempList != null)
                    {
                        lastMediaName = tempList;
                    }
                    if (name != null)
                    {
                        currentName = name.Name;
                    }
                }
                UpdateView(lastMediaName, currentName, m_Memory[count - 1]);
                m_Memory.RemoveAt(count - 1);
            }
            else
            {
                if (m_LeftAlphabet != null)
                    m_LeftAlphabet.Close();
                if (m_RightAlphabet != null)
                    m_RightAlphabet.Close();
                Close();
            }
        }

        private void UpdateView(List<IMediaName> media, string currentScreen, int selectedIndex)
        {
            m_Listbox.UpdateData(media);
            base.HeaderText = currentScreen;
            m_Listbox.SelectedItemIndex = selectedIndex;
            m_Listbox.EnsureVisible(selectedIndex);
        }
    }
}
